package com.lnsf.bookstore.system.book.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.annotation.SysLog;
import com.lnsf.bookstore.system.book.service.BookService;
import com.lnsf.bookstore.system.book.vo.Book;
import com.lnsf.bookstore.common.dto.ResultFailDto;
import com.lnsf.bookstore.common.dto.ResultSuccessDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/v_list.do")
    public String list(HttpServletRequest request, ModelMap modelMap){
        System.out.println("book界面.....");
        return "admin/book/book";
    }

    /**
     * 查看所有列表
     */
    @SysLog("查看商品列表")
    @RequestMapping("/queryAll")
    @ResponseBody
    public Object queryAll(@RequestParam Map<String, Object> params, Book book) {
        PageInfo<Book> pageInfo = bookService.selectAll(params,book);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page",pageInfo);
        return map;
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    @SysLog("查看商品信息")
    //@RequiresPermissions("category:info")
    public Object info(@PathVariable("id") Integer id) {
        Book book = bookService.queryObject(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("book",book);
        return map;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @SysLog("删除商品")
    //@RequiresPermissions("category:info")
    public Object delete(@RequestBody Integer[] ids) {
        if (bookService.deleteBatch(ids) > 0) {
            return new ResultSuccessDto("删除成功");
        }else {
            return  new ResultFailDto("删除失败");
        }
    }


    /**
     * 修改、新增操作
     */
    @SysLog("修改/新增商品")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Object saveOrUpdate(HttpServletRequest request, Integer id , @RequestBody Book book, ModelMap modelMap){
        if(book.getId()==null){//新增
            //verifyForm(category);
            bookService.save(book);
        }else{//修改
            //verifyForm(category);
            bookService.update(book);
        }
        return new ResultSuccessDto("操作成功");
    }

    /**
     * 上架
     */
    @RequestMapping("/enSale")
    @ResponseBody
    public Object enSale(@RequestBody Integer id) {
        Book book = new Book();
        book.setId(id);
        book.setIsOnSale(1);
        bookService.update(book);
        return new ResultSuccessDto("上架成功");
    }

    /**
     * 下架
     */
    @RequestMapping("/unSale")
    @ResponseBody
    public Object unSale(@RequestBody Integer id) {
        Book book = new Book();
        book.setId(id);
        book.setIsOnSale(0);
        bookService.update(book);
        return new ResultSuccessDto("下架成功");
    }

}
